package com.gxa.jjty.admin.controller;

import com.alibaba.excel.EasyExcel;

import com.gxa.jjty.admin.service.HandicappedService;
import com.gxa.jjty.core.pojo.Handicapped;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;

/**
 * Created with IntelliJ IDEA.
 *
 * @author : 宸濯
 * Date: 2021/10/28 19:30
 * Description:
 * Version: V1.0
 */
@Api(tags = "导出接口api")
@CrossOrigin
@RestController
public class ExportController {
    @Autowired
    HandicappedService handicappedService;

    @GetMapping("/exportHandicapped")
    @ApiOperation("导出表格")

    public void storesAllocating( HttpServletResponse response,String from,String to,String code,String name,String phone,String state) throws  Exception {
        System.out.println("导出表格");
        String username ="残疾人导出表格";
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyExcel没有关系
        String fileName = URLEncoder.encode(username, "UTF-8")
                .replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里需要设置不关闭流
        EasyExcel.write(response.getOutputStream(), Handicapped.class)
                .sheet("sheet1").doWrite( handicappedService.selectByFiedl(from,to,code,name,phone,state));
    }

}